import React from 'react'
export const authModes = { //权限模式
    showOrHide: "showOrHide",//显示或隐藏
    openOrDisabled: "openOrDisabled" //开启或禁用
}
/* 
auth:权限
children:节点或组件
authMode:权限模式 （默认显示或隐藏）
rest:其它属性
*/
export function AuthComponent({ auth, children, authMode = authModes.showOrHide, ...rest }) {
    const permissions = JSON.parse(localStorage.permissions) //当前登录用户权限
    const has = permissions.includes(auth) //是否拥有该权限
    if (authMode == "showOrHide") return has ? React.cloneElement(children, rest) : null
    else if (authMode == "openOrDisabled") return has ? React.cloneElement(children, rest) : React.cloneElement(children, { disabled: true, ...rest })
}
//React.cloneElement(节点,传递给节点的属性) //克隆一个节点